package com.example.springboot1.api;

import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class RichTextToWordUsingPoiTl {
    public static void main(String[] args) {
//        // 模板文件路径
//        String templatePath = "F:/template.docx";
//        // 输出文件路径
//        String outputPath = "F:/output.docx";
//
//        try {
//            // 加载模板文件
//            InputStream templateInputStream = new FileInputStream(templatePath);
//            XWPFDocument document = new XWPFDocument(templateInputStream);
//            XHTMLOptions options = XHTMLOptions.create();
//            // 获取模板中的占位符段落
//            String placeholder = "${text}";
//            int pos = findPlaceholderParagraph(document, placeholder);
//            OutputStream outputStream = new FileOutputStream(outputPath);
//            if (pos != -1) {
//                // 替换占位符段落为富文本内容
//                String richText = "<h1>This is a heading</h1><p>This is a paragraph.</p><ul><li>Item 1</li><li>Item 2</li></ul>";
//                InputStream inputStream = new ByteArrayInputStream(richText.getBytes());
//                XHTMLConverter.getInstance().convert(document, outputStream, options);
//            }
//
//            // 保存输出文件
//            document.write(outputStream);
//            outputStream.close();
//            document.close();
//
//            System.out.println("富文本内容已写入Word模板!");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
            test();


    }

    public static void test(){
        // 富文本内容
        String richText = "<h1>This is a heading</h1><p>This is a paragraph.</p><ul><li>Item 1</li><li>Item 2</li></ul>";

        // 创建XWPFDocument对象
        XWPFDocument document = new XWPFDocument();

        // 添加富文本内容到Word文档
        XWPFParagraph paragraph = document.createParagraph();
        try {
            InputStream inputStream = new ByteArrayInputStream(richText.getBytes());
            XWPFRun run = paragraph.createRun();
            XHTMLOptions options = XHTMLOptions.create();
            // 使用Poi-tl提供的XHTMLConverter将HTML转换为Word格式
            FileOutputStream out = new FileOutputStream("F:/file.docx");

            XHTMLConverter.getInstance().convert(document, out, options);
            document.write(out);
            out.close();
            document.close();
            // 如果需要，你还可以设置其他样式，如字体、大小等
            run.setFontSize(12);
            run.setFontFamily("Arial");
        } catch (IOException e) {
            e.printStackTrace();
        }



    }

    private static int findPlaceholderParagraph(XWPFDocument document, String placeholder) {
        int pos = -1;
        for (int i = 0; i < document.getParagraphs().size(); i++) {
            String text = document.getParagraphs().get(i).getText();
            if (text.contains(placeholder)) {
                pos = i;
                break;
            }
        }
        return pos;
    }
}